<?php
/**
 * Zend Framework Contribution
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category   Zend
 * @package    Zend_Mwb
 * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 * @version    1.0 Mwb.php 2012-05-01 Sergio DE VASCONCELOS
 */

/**
 * MySQL Workbench document
 *
 * Class is used to load existing MWB document.
 *
 * @category   Zend
 * @package    Zend_Mwb
 */
class Zend_Mwb
{
	/**** Class Constants ****/

	/**
	 * Version number of generated MWB documents.
	 */
	const MWB_VERSION = '1.4.2';

	/**
	 * TODO : rename
	 * @var Zend_Mwb_Document|Zend_Mwb_Dom_Document
	 */
	public $document;
	public function getDocument() {
		return $this->document;
	}
	public function setDocument($document) {
		$this->document = $document;
		return $this;
	}
	
	/**
     * Load MWB document from a file
     * $source = null, $revision = null, $load = false
     *
     * @param string $source
     * @param integer $revision
     * @return Zend_Mwb
     */
    static public function load($source = null, $revision = null)
    {
		$zip = new ZipArchive;
		if ($zip->open($source) === TRUE) {
			$xml = $zip->getFromName('document.mwb.xml');
			$zip->close();
		}
    	//$filename = dirname($source) . '/' . basename($source, ".mwb") . '/document.mwb.xml';
    	
    	$domDocument = new Zend_Mwb_DomDocument('1.0', 'ISO-8859-1');
    	$domDocument->registerNodeClass('DOMNode',    'Zend_Mwb_DomNode');
		$domDocument->registerNodeClass('DOMElement', 'Zend_Mwb_DomElement');
		$domDocument->registerNodeClass('DOMDocument','Zend_Mwb_DomDocument');
		//$domDocument->validateOnParse = true; use getElementById
		//$domDocument->load($filename);
		$domDocument->loadXML($xml);
    	
    	$document = new Zend_Mwb_Dom_Document($domDocument);
	    
    	$mwb = new Zend_Mwb();
    	$mwb->document = $document;
    	
    	return $mwb;
    }
    /**
     * Render MWB document and save it.
     *
     * If $updateOnly is true and it's not a new document, then it only
     * appends new section to the end of file.
     *
     * @param string $filename
     * @param boolean $updateOnly
     * @throws Zend_Mwb_Exception
     */
    public function save($filename, $updateOnly = false)
    {
    	/// TODO : placer dans le render() un helper qui check le fopen
    	/*
        if (($file = @fopen($filename, $updateOnly ? 'ab':'wb')) === false ) {
            require_once 'Zend/Mwb/Exception.php';
            throw new Zend_Mwb_Exception( "Can not open '$filename' file for writing." );
        }
		*/
        //$this->render($updateOnly, $file);
    	/*
        fclose($file);
		*/
    }
	
	/**
	 // create a formatter
	 $formatter = new \MwbExporter\Formatter\Zend\DbTable\Loader($setup);

	 // parse the mwb file
	 $mwb = new \MwbExporter\Core\Workbench\Document('data/test.mwb', $formatter);

	 // show the export output of the mwb file
	 echo $mwb->display();
	 
	 // ---------------------------------------
	
	 $mwb = Zend_Mwb::load('data/test.mwb');
	 	  //Zend_Mwb::parse('<xml>data/test/document.mwb.xml</xml>');
	 $mwb->save();
	 $mwb->save($filename);
	 
	 
	 $mwb->db->query('');
	 $mwb->document
	 	->getPhysicalModels()->current()
	 	->getCatalogs()->current()
	 	->getTables()->current()
	 	->getColumns()->current()
	 	->getForeignKeys()->current()
	 	->getName();
	 
		//Zend_Mwb_Render::xxx($_);
	 	
	 	
	 */

    /**
     * Creates or loads MWB document.
     *
     * TODO: If $source is null, then it creates a new document.
     *
     * TODO: If $source is a string and $load is false, then it loads document
     * from a binary string.
     *
     * If $source is a string and $load is true, then it loads document
     * from a file.
     * $revision used to roll back document to specified version
     * (0 - current version, 1 - previous version, 2 - ...)
     *
     * @param string  $source - MWB file to load
     * @param integer $revision
     * @throws Zend_Mwb_Exception
     * @return Zend_Mwb
     */
	public function __construct($source = null, $revision = null, $load = false)
	{
    	
//        require_once 'Zend/Mwb/ElementFactory.php';
//        $this->_objFactory = Zend_Mwb_ElementFactory::createFactory(1);

        if ($source !== null) {
//            $this->_parser           = new Zend_Mwb_Parser($source, $this->_objFactory, $load);
            //$this->_pdfHeaderVersion = $this->_parser->getPDFVersion();
        } else {
            //throw new Zend_Mwb_Exception('Can create empty document', Zend_Mwb_Exception::NOT_IMPLEMENTED);
        }
    }
	
}

class Zend_Mwb_DomNode extends DOMNode
{
	/**
	 * @var Zend_Mwb_Dom_Interface
	 */
	protected $dom;
	/**
	 * @return Zend_Mwb_Dom_Interface
	 */
	public function getDom() {
		return $this->dom;
	}
	public function hasDom() {
		return isset($this->dom);
	}
	/**
	 * @param Zend_Mwb_Dom_Interface
	 * @return Zend_Mwb_DomElement
	 */
	public function setDom($dom) {
		$this->dom = $dom;
		return $this;
	}
}
class Zend_Mwb_DomElement extends DOMElement
{
	/**
	 * @var Zend_Mwb_Dom_Interface
	 */
	protected $dom;
	/**
	 * @return Zend_Mwb_Dom_Interface
	 */
	public function getDom() {
		return $this->dom;
	}
	public function hasDom() {
		return isset($this->dom);
	}
	/**
	 * @param Zend_Mwb_Dom_Interface
	 * @return Zend_Mwb_DomElement
	 */
	public function setDom($dom) {
		$this->dom = $dom;
		return $this;
	}
}

class Zend_Mwb_DomDocument extends DOMDocument
{
	/**
	 * @var Zend_Mwb_Dom_Interface
	 */
	protected $dom;
	/**
	 * @return Zend_Mwb_Dom_Interface
	 */
	public function getDom() {
		return $this->dom;
	}
	public function hasDom() {
		return isset($this->dom);
	}
	/**
	 * @param Zend_Mwb_Dom_Interface
	 * @return Zend_Mwb_DomElement
	 */
	public function setDom($dom) {
		$this->dom = $dom;
		return $this;
	}
}
